package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.mapper.CourseBaseMapper;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.mapper.TeachplanWorkMapper;
import com.xuecheng.content.model.dto.CourseTeacherDto;
import com.xuecheng.content.model.po.CourseBase;
import com.xuecheng.content.model.po.CourseTeacher;
import com.xuecheng.content.model.po.Work;
import com.xuecheng.content.service.CourseTeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class CourseTeacherServiceImpl implements CourseTeacherService {
    @Autowired
    private CourseTeacherMapper courseTeacherMapper;
    @Autowired
    private CourseBaseMapper courseBaseMapper;
    @Autowired
    TeachplanWorkMapper teachplanWorkMapper;

    @Override
    public List<CourseTeacher> findCourseTeacherList(Long courseId) {
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getCourseId, courseId);
        List<CourseTeacher> courseTeacherList = courseTeacherMapper.selectList(queryWrapper);
        return courseTeacherList;

    }

    @Override
    public CourseTeacher saveorEdit(Long companyId,CourseTeacherDto courseTeacherDto) {
        if (companyId == null) {
            XueChengPlusException.cast("机构id为空");
        }
        if (courseTeacherDto == null) {
            XueChengPlusException.cast("课程教师信息为空");
        }
        Long courseId = courseTeacherDto.getCourseId();
        CourseBase courseBase1 = courseBaseMapper.selectById(courseId);
        if(!Objects.equals(companyId,courseBase1.getCompanyId()))
        {
            XueChengPlusException.cast("只允许向机构自己的课程中添加老师");
        }
        // 获取课程基本信息
        CourseBase courseBase = courseBaseMapper.selectById(courseTeacherDto.getCourseId());
        if (courseBase == null) {
            XueChengPlusException.cast("课程不存在");
        }

        // 检查课程ID是否匹配
        if (!Objects.equals(courseBase.getId(), courseTeacherDto.getCourseId())) {
            XueChengPlusException.cast("课程id不匹配");
        }

        try {
            if (courseTeacherDto.getId() == null) {
                // 新增课程教师信息
                CourseTeacher courseTeacher = new CourseTeacher();
                BeanUtils.copyProperties(courseTeacherDto, courseTeacher);
                courseTeacher.setCreateDate(LocalDateTime.now());
                courseTeacherMapper.insert(courseTeacher);
                return courseTeacher;
            } else {
                // 更新课程教师信息
                CourseTeacher courseTeacher = courseTeacherMapper.selectById(courseTeacherDto.getId());
                if (courseTeacher == null) {
                    XueChengPlusException.cast("课程教师信息不存在");
                }
                BeanUtils.copyProperties(courseTeacherDto, courseTeacher);
                courseTeacherMapper.updateById(courseTeacher);
                return courseTeacher;
            }
        } catch (Exception e) {
            // 记录异常信息
            log.error("保存或编辑课程教师信息失败: {}", e.getMessage(), e);
            throw new XueChengPlusException("保存或编辑课程教师信息失败");
        }
    }

    @Override
    public void deleteTeacher( Long companyId,Long courseId, Long id) {
        //校验课程id和教师id是否为空
        if (courseId == null || id == null) {
            XueChengPlusException.cast("课程id和教师id不能为空");
        }
        CourseBase courseBase = courseBaseMapper.selectById(courseId);
        if(!Objects.equals(companyId,courseBase.getCompanyId()))
        {
            XueChengPlusException.cast("只允许向机构自己的课程中删除老师。");
        }
        //校验课程id和教师id是否匹配
        CourseTeacher courseTeacher = courseTeacherMapper.selectById(id);
        if (courseTeacher == null) {
            XueChengPlusException.cast("教师信息不存在");
        }
       if (!Objects.equals(courseId,courseTeacher.getCourseId()))
       {
           XueChengPlusException.cast("课程id和教师id不匹配");
       }
       courseTeacherMapper.deleteTeacherBycourseIdAndId(courseId,id);
    }

    @Override
    public CourseTeacher selectBycourseId(Long courseId) {

        if (courseId==null)
        {
            XueChengPlusException.cast("课程id为空");
        }
        return courseTeacherMapper.selectByCourseId(courseId);

    }

    @Override
    public  List<Work> findWorkByCourseId(Long courseId) {
        if (courseId==null)
        {
            XueChengPlusException.cast("课程id为空");
        }
        return teachplanWorkMapper.selectByCourId(courseId);
    }

}
